package com.myzl.web.controller.coal;

import com.myzl.coal.domain.MessageSourceRelation;
import com.myzl.coal.service.IMessageSourceRelationService;
import com.myzl.common.annotation.Log;
import com.myzl.common.core.controller.BaseController;
import com.myzl.common.core.domain.AjaxResult;
import com.myzl.common.core.page.TableDataInfo;
import com.myzl.common.enums.BusinessType;
import com.myzl.common.utils.SecurityUtils;
import com.myzl.common.utils.poi.ExcelUtil;
import com.myzl.web.controller.coal.request.MessageSourceRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户订阅消息源Controller
 *
 * @author myzl
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/coal/messageSourceRelation")
public class MessageSourceRelationController extends BaseController {
    @Autowired
    private IMessageSourceRelationService messageSourceRelationService;

    /**
     * 查询用户订阅消息源列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSourceRelation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageSourceRelation messageSourceRelation) {
        startPage();
        List<MessageSourceRelation> list = messageSourceRelationService.selectMessageSourceRelationList(messageSourceRelation);
        return getDataTable(list);
    }

    /**
     * 导出用户订阅消息源列表
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSourceRelation:export')")
    @Log(title = "用户订阅消息源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageSourceRelation messageSourceRelation) {
        List<MessageSourceRelation> list = messageSourceRelationService.selectMessageSourceRelationList(messageSourceRelation);
        ExcelUtil<MessageSourceRelation> util = new ExcelUtil<MessageSourceRelation>(MessageSourceRelation.class);
        util.exportExcel(response, list, "用户订阅消息源数据");
    }

    /**
     * 获取用户订阅消息源详细信息
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSourceRelation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(messageSourceRelationService.selectMessageSourceRelationById(id));
    }

    /**
     * 新增用户订阅消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSource:relation')")
    @Log(title = "用户订阅消息源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MessageSourceRelation messageSourceRelation) {
        return toAjax(messageSourceRelationService.insertMessageSourceRelation(messageSourceRelation));
    }

    @PreAuthorize("@ss.hasPermi('coal:messageSource:relation')")
    @Log(title = "用户订阅消息源", businessType = BusinessType.INSERT)
    @DeleteMapping("/delete")
    public AjaxResult deleteRelation(@RequestBody MessageSourceRelation messageSourceRelation) {
        messageSourceRelation.setUserId(SecurityUtils.getUserId());
        return AjaxResult.success(messageSourceRelationService.deleteByMessageSourceId(messageSourceRelation));
    }


    /**
     * 修改用户订阅消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSourceRelation:edit')")
    @Log(title = "用户订阅消息源", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}")
    public AjaxResult edit(@RequestBody MessageSourceRelation messageSourceRelation) {
        return toAjax(messageSourceRelationService.updateMessageSourceRelation(messageSourceRelation));
    }

    /**
     * 删除用户订阅消息源
     */
    @PreAuthorize("@ss.hasPermi('coal:messageSourceRelation:remove')")
    @Log(title = "用户订阅消息源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageSourceRelationService.deleteMessageSourceRelationByIds(ids));
    }

    @GetMapping(value = "/getSubscribe/{messageSourceId}")
    public AjaxResult subscribeRelation(@PathVariable("messageSourceId") Long messageSourceId) {
        return AjaxResult.success(messageSourceRelationService.subscribeRelation(messageSourceId));
    }

    @PreAuthorize("@ss.hasPermi('coal:messageSource:subscribe')")
    @Log(title = "保存多用户订阅消息源", businessType = BusinessType.INSERT)
    @PostMapping("/subscribe")
    public AjaxResult subscribe(@RequestBody MessageSourceRequest request) {
        return toAjax(messageSourceRelationService.subscribe(request.getMessageSourceId(), request.getUserIds()));
    }
}
